#pragma once

#include<unordered_map>
#include<iostream>
#include"log.hpp"
#include<fstream>

extern Log lg;

const std::string dictpath = "./dict.txt";
const std::string sep = ":";

bool Split(const std::string& str,std::string& part1,std::string& part2)
{
    auto pos = str.find(sep);
    if(pos == std::string::npos) return false;

    part1 = str.substr(0,pos);
    part2 = str.substr(pos+1);
    return true;
}


class init
{
public:
    init()
    {
        std::ifstream in(dictpath);
        if(!in.is_open())
        {
            lg(Fatal,"file %s open error",dictpath);
            exit(1);
        }

        std::string str;
        while(std::getline(in,str))
        {
            std::string part1;
            std::string part2;
            Split(str,part1,part2);
            dict.insert({part1,part2});
        }
        in.close();

    }

    std::string translation(const std::string& word)
    {
        auto iter = dict.find(word);
        if(iter == dict.end()) return "UnKnown";
        return iter->second;
    }
private:
    std::unordered_map<std::string,std::string> dict;
};